<template>
	<div class="">

	<avue-crud ref="crud"
			:page="page"
			:data="billsDataList"
			:table-loading="dataListLoading"
			:option="tableOption"
			@on-load="getBills">
	</avue-crud>


	</div>
</template>

<script>
import { tableOption } from '@/crud/shop/couponHistory'
export default {
	data() {
		var validateCashOutAmount = (rule,value,callback)	=>{
			if (this.dataForm.cashOutAmount>this.amount) {
				callback(new Error('超出最大提现额度'))
			}
		}

		return {
			amount:0.00,
			visible: false,
			isWithdrawShow : false,
			tableOption: tableOption,
			billsDataList: [],
			dataListLoading: false,
			loading: false,
			dataForm:{
				cashOutAmount: 0
			},

      searchType:'',
      searchOrderNum: '',
			searchTimeRange: [],
			
      payStateOptions:[{
        label:'待支付',
        value:{orderState:0, status:1,}
      },{
        label:'待支付',
        value:{orderState:0, status:1,}
      },{
        label:'待支付',
        value:{orderState:0, status:1,}
      },],
			
			page: {
					total: 0, // 总页数
					currentPage: 1, // 当前页数
					size: 10 // 每页显示多少条
			},
			myinfo:{},
			dataRule:{
				cashOutAmount:[
					{required: true, message:'金额不能为空', trigger:'blur'},
					{validator: validateCashOutAmount, trigger:'blur' }
				]
			}
		}
	},

    // activated(){
    //     this.getBills(this.page)
    // },

	methods:{
		getBills(page){
			this.getAmount()
			page = (page === undefined? this.page: page)
			// this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/coupon/coupon/couponUsePage'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign({
						current: page == null? this.page.currentPage: page.currentPage,
						size: page == null? this.page.size: page.pageSize,
					},this.params),false
				)
			}).then(({data}) =>{
				this.billsDataList = data.records
				this.page.total = data.total
				this.dataListLoading = false
			})
		},

		/**
		 * 刷新回调
		 */

		searchChange (params) {
			if (params.time&&params.time.length !== 1) {     
				params.startTime= this.dateTimeString(params.time[0]),
				params.endTime= this.dateTimeString(params.time[1])
			}
			delete params.time
			this.getBills(this.page, params)
		},

		
    handleSearch(state){
      if (state==0) {
        this.searchOrderNum = ''
        this.searchOrderState = ''
        this.searchTimeRange = []
        this.getBills()
      }else if (state == 1) {
				var params = {
					orderNumber: this.searchOrderNum,
					startTime: this.searchTimeRange.length!==0? this.dateTimeString(this.searchTimeRange[0]):'',
					endTime: this.searchTimeRange.length!==0? this.dateTimeString(this.searchTimeRange[1]):'',
					type: this.searchType
				}
				this.params = params
        this.getBills()
      }
    },
		//格式化日期时间
		dateTimeString(val){
			let date = new Date(val)
			let D = [
			String(date.getFullYear()),
			date.getMonth()<9?'0'+(date.getMonth()+1):String(date.getMonth()+1),
			date.getDate()<10?'0'+date.getDate():String(date.getDate()),
			date.getHours()<10?'0'+date.getHours():String(date.getHours()),
			date.getMinutes()<10?'0'+date.getMinutes():String(date.getMinutes()),
			date.getSeconds()<10?'0'+date.getSeconds():String(date.getSeconds())
			]
			return D.slice(0,3).join('-')+' '+D.slice(3).join(':')  
		},

		//获取当前账户余额
		getAmount(){
			this.$http({
				url:this.$http.adornUrl('/finance/getAccount'),
			}).then(({data}) =>{
				this.amount = data%1 === 0?String(data)+'.00':String(data)
			})
		},

		//提现
		withdraw(){
			if (this.dataForm.cashOutAmount>0&&this.dataForm.cashOutAmount<this.amount) {
				this.$http({
					url:this.$http.adornUrl('/cashOut/cashoutByShop'),
					params: this.$http.adornParams({
						cashOutMoney : this.dataForm.cashOutAmount
					})
				}).then(()=>{
						this.$message({
							type:'sucess',
							message:"申请已提交，请等待汇款"
					})
						this.visible = false
				})
			}
		},
		openWithdrawWindow(){
			this.loading = true
      this.$http({
				url:this.$http.adornUrl("/shop/shopDetail/getShopDetailInformation"),
        method: 'get',
      }).then(({data}) =>{
				if (data.cardNumber) {
					this.visible = true
					this.loading = false
					this.myinfo = data
				}else{
					this.$message.error('您未绑定银行账户，请先去“个人中心”绑定银行账户')
				}
      }) 
			

		},				

	},

}
</script>
<style lang="scss" scoped>
// 自定义样式
.avue-crud__title{
    justify-content: flex-start;
}
.el-range-editor--small .el-range-separator{
  width: 30px;
}
.avue-crud .avue-crud__left .el-button, .avue-crud .avue-crud__right .el-button, .avue-form .avue-crud__left .el-button, .avue-form .avue-crud__right .el-button {
    margin-right: 8px;
    margin-bottom: 8px;
    height: 34px;
    align-self: flex-end;
}

//提现盒子样式
.Box{
    display: flex;
}
.withdrawBox{
    display: flex;
    align-items: center;
    height: 80px;
}
.showAmount{
    height: 80px;
    line-height: 80px;
    width: 70%;
}
.withdrawBtn{
    height: 40px;
    width: 100px;
    border: 0;
    border-radius: 3px;
    background-color: #02A1E9;
    text-align: center;   
    line-height: 40px;
    color: white;
    margin-left: 50px;
    position: relative;
    top: 10px;
}
.el-form-item{
	margin-bottom: 5px;
}
// .el-input {
//     width: 364px;
// }
</style>

